package com.elinkway.infinitemovies.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.elinkway.infinitemovies.utils.LogUtils;
import com.lvideo.http.bean.LVideoBaseBean;

import java.lang.Thread.UncaughtExceptionHandler;

public abstract class BaseDao<T extends LVideoBaseBean> {

    protected DBHelper helper;

    protected static final DaoThread mIOThread;

    static {
        mIOThread = new DaoThread();
        mIOThread.setName("dao-thread");
        mIOThread.setPriority(Thread.NORM_PRIORITY - 1);
        mIOThread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {

            @Override
            public void uncaughtException(Thread thread, Throwable ex) {
                ex.printStackTrace();
            }

        });
        mIOThread.start();
    }

    public BaseDao(Context context) {
        helper = new DBHelper(context);
    }

    public abstract void delete(String pk);

    public abstract void delAll();

    public abstract void save(T bean);

    public void update(T prior, T now) {

    }

    public void doInBackground(Runnable r) {
        mIOThread.post(r);
    }

    protected synchronized SQLiteDatabase safelyGetDataBase() {
        LogUtils.i("database", "open");
        return DatabaseManager.getInstance().openDatabase();
    }

    protected synchronized void safelyCloseDataBase() {
        LogUtils.e("database", "close");
        DatabaseManager.getInstance().closeDatabase();
    }

}
